﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// QuestionFilterDialog.xaml 的交互逻辑
    /// </summary>
    public partial class QuestionFilterDialog : MetroWindow
    {
        public static string CustomTitle
        {
            get
            {
                return Globals.AppName + " - " + "试题筛选器";
            }
        }

        public QuestionFilterDialog(List<QuestionList> qlList)
        {
            InitializeComponent();

            this.qlList = qlList;
        }

        private List<QuestionList> qlList;

        private void btnExportToActiveEditor_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(cmbFilter.Text))
            {
                foreach (var item in lbxFindedQuestions.Items)
                {
                    var card = item as QuestionCard;
                    if (card != null)
                        card.Visibility = Visibility.Visible;
                }
            }
            else
            {
                cmbFilter.Items.Insert(0, cmbFilter.Text);
                foreach (var item in lbxFindedQuestions.Items)
                {
                    var card = item as QuestionCard;
                    if (card != null)
                    {
                        if (card.MasterQuestion.OutText().Contains(cmbFilter.Text))
                        {
                            card.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            card.Visibility = Visibility.Collapsed;
                        }
                    }
                }
            }
        }

        private void lbxFindedQuestions_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            foreach (var ue in lbxFindedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card != null)
                    card.MaxWidth = Math.Max(0, lbxFindedQuestions.ActualWidth - 36);
            }
        }

        private void lbxCheckedQuestions_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            foreach (var ue in lbxCheckedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card != null)
                    card.MaxWidth = Math.Max(0, lbxCheckedQuestions.ActualWidth - 36);
            }
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            int num = 1;
            foreach (var qlist in qlList)
            {
                foreach (var q in qlist)
                {
                    q.OrderNumber = num;
                    num++;

                    if (string.IsNullOrWhiteSpace(q.MarkdownLinkText) == false)
                    {
                        q.MarkdownImageLinkRealPath =
                            Utils.ImageLinkTool.GetRealPathFromImagelink(q.MarkdownLinkText, qlist.FilePath);
                        var ae = Globals.MainWindow.ActivedEditor;
                        if (ae != null)
                        {
                            q.NewMarkdownLinkText = $"![{q.MarkdownIamgeHeader}]({Utils.ImageLinkTool.GetImageRelativePath(q.MarkdownImageLinkRealPath, ae.FullFilePath)})";
                        }
                    }

                    var qCard = new QuestionCard(q)
                    {
                        IsChecked = false,
                        MaxWidth = lbxFindedQuestions.ActualWidth - 36,
                    };
                    lbxFindedQuestions.Items.Add(qCard);
                }
            }
        }

        private void lbxFindedQuestions_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            ItemsControl items = (ItemsControl)sender;
            ScrollViewer scroll = FindVisualChild<ScrollViewer>(items);
            if (scroll != null)
            {
                scroll.ScrollToVerticalOffset(scroll.VerticalOffset - (e.Delta < 0 ? -1 : 1));  // 只要1就行了，这里的1表示一个Item的高度。
                e.Handled = true;
            }
        }

        private void lbxCheckedQuestions_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            ItemsControl items = (ItemsControl)sender;
            ScrollViewer scroll = FindVisualChild<ScrollViewer>(items);
            if (scroll != null)
            {
                scroll.ScrollToVerticalOffset(scroll.VerticalOffset - (e.Delta < 0 ? -1 : 1));  // 只要1就行了，这里的1表示一个Item的高度。
                e.Handled = true;
            }
        }

        #region 查找子控件，以支持鼠标滚轮 
        /// <summary>
        /// CopyFrom:https://www.cnblogs.com/mumuliang/archive/2013/11/19/3431716.html
        /// 这个方法貌似比之前找的那个更好用些。
        /// </summary>
        public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            if (obj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }
                    T childItem = FindVisualChild<T>(child);
                    if (childItem != null) return childItem;
                }
            }
            return null;
        }
        //void lbMatFile_MouseWheel(object sender, MouseWheelEventArgs e)
        //{
        //    ItemsControl items = (ItemsControl)sender;
        //    ScrollViewer scroll = FindVisualChild<ScrollViewer>(items);
        //    if (scroll != null)
        //    {
        //        scroll.ScrollToHorizontalOffset(scroll.HorizontalOffset - e.Delta);
        //    }
        //}
        #endregion

        private void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            var lst = new List<QuestionCard>();
            for (int i = lbxFindedQuestions.Items.Count - 1; i >= 0; i--)
            {
                var ue = lbxFindedQuestions.Items[i];
                var card = ue as QuestionCard;
                if (card == null) continue;

                if (card.IsChecked == true)
                {
                    lbxFindedQuestions.Items.RemoveAt(i);
                    lst.Add(card);
                }
            }

            for (int i = lst.Count - 1; i >= 0; i--)
            {
                lbxCheckedQuestions.Items.Add(lst[i]);
            }
        }

        private void btnUnSelect_Click(object sender, RoutedEventArgs e)
        {
            var lst = new List<QuestionCard>();
            for (int i = lbxCheckedQuestions.Items.Count - 1; i >= 0; i--)
            {
                var ue = lbxCheckedQuestions.Items[i];
                var card = ue as QuestionCard;
                if (card == null) continue;

                if (card.IsChecked == true)
                {
                    lbxCheckedQuestions.Items.RemoveAt(i);
                    lst.Add(card);
                }
            }

            for (int i = lst.Count - 1; i >= 0; i--)
            {
                lbxFindedQuestions.Items.Add(lst[i]);
            }
        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {
            var selItem = lbxCheckedQuestions.SelectedItem as QuestionCard;
            if (selItem == null) return;

            var index = lbxCheckedQuestions.Items.IndexOf(selItem);
            if (index >= 1)
            {
                lbxCheckedQuestions.Items.RemoveAt(index);
                lbxCheckedQuestions.Items.Insert(index - 1, selItem);
                selItem.IsSelected = true;
                lbxCheckedQuestions.ScrollIntoView(selItem);
            }
        }

        private void btnBottom_Click(object sender, RoutedEventArgs e)
        {
            var selItem = lbxCheckedQuestions.SelectedItem as QuestionCard;
            if (selItem == null) return;

            var index = lbxCheckedQuestions.Items.IndexOf(selItem);
            if (index < lbxCheckedQuestions.Items.Count - 1)
            {
                lbxCheckedQuestions.Items.RemoveAt(index);
                lbxCheckedQuestions.Items.Add(selItem);
                selItem.IsSelected = true;
                lbxCheckedQuestions.ScrollIntoView(selItem);
            }
        }

        private void btnDown_Click(object sender, RoutedEventArgs e)
        {
            var selItem = lbxCheckedQuestions.SelectedItem as QuestionCard;
            if (selItem == null) return;

            var index = lbxCheckedQuestions.Items.IndexOf(selItem);
            if (index < lbxCheckedQuestions.Items.Count - 1)
            {
                lbxCheckedQuestions.Items.RemoveAt(index);
                lbxCheckedQuestions.Items.Insert(index + 1, selItem);
                selItem.IsSelected = true;
                lbxCheckedQuestions.ScrollIntoView(selItem);
            }
        }

        private void btnTop_Click(object sender, RoutedEventArgs e)
        {
            var selItem = lbxCheckedQuestions.SelectedItem as QuestionCard;
            if (selItem == null) return;

            var index = lbxCheckedQuestions.Items.IndexOf(selItem);
            if (index >= 1)
            {
                lbxCheckedQuestions.Items.RemoveAt(index);
                lbxCheckedQuestions.Items.Insert(0, selItem);
                selItem.IsSelected = true;
                lbxCheckedQuestions.ScrollIntoView(selItem);
            }
        }

        private void btnSelectAllFinded_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxFindedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                card.IsChecked = true;
            }
        }

        private void btnUnSelectAllFinded_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxFindedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                card.IsChecked = false;
            }
        }

        private void btnSwitchFinded_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxFindedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                if (card.IsChecked == true)
                {
                    card.IsChecked = false;
                }
                else card.IsChecked = true;
            }
        }

        private void btnSelectAllChecked_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxCheckedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                card.IsChecked = true;
            }
        }

        private void btnUnSelectAllChecked_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxCheckedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                card.IsChecked = false;
            }
        }

        private void btnSwitchChecked_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxCheckedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                if (card.IsChecked == true)
                {
                    card.IsChecked = false;
                }
                else card.IsChecked = true;
            }
        }

        private void cmbFilter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnFilter_Click(sender, e);
            }
        }

        private void miShowAnaLys_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxFindedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                card.ShowAnswerOrAnalisys();
            }
        }

        private void miHideAnalys_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxFindedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                card.HideAnswerOrAnalisys();
            }
        }
        private void miShowAnaLys2_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxCheckedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                card.ShowAnswerOrAnalisys();
            }
        }

        private void miHideAnalys2_Click(object sender, RoutedEventArgs e)
        {
            foreach (var ue in lbxCheckedQuestions.Items)
            {
                var card = ue as QuestionCard;
                if (card == null) continue;

                card.HideAnswerOrAnalisys();
            }
        }
    }
}
